package com.weaver.qfengx.map;

import com.google.common.base.Joiner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.*;

public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Serializable, Cloneable {

    private static final long serialVersionUID = 1L;

    /**
     * 默认增加因子
     */
    protected static final float DEFAULT_LOAD_FACTOR = .75f;

    /**
     * 默认初始大小
     */
    protected static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;

    private final Map<K, V> raw;

    public MapWrapper(Map<K, V> raw) {
        this.raw = raw;
    }

    public Map<K, V> getRaw() {
        return this.raw;
    }

    @NotNull
    @Override
    public Iterator<Entry<K, V>> iterator() {
        return this.entrySet().iterator();
    }

    @Override
    public int size() {
        return raw.size();
    }

    @Override
    public boolean isEmpty() {
        return raw.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return raw.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return raw.containsValue(value);
    }

    @Override
    public V get(Object key) {
        return raw.get(key);
    }

    @Nullable
    @Override
    public V put(K key, V value) {
        return raw.put(key, value);
    }

    @Override
    public V remove(Object key) {
        return raw.remove(key);
    }

    @Override
    public void putAll(@NotNull Map<? extends K, ? extends V> m) {
        raw.putAll(m);
    }

    @Override
    public void clear() {
        raw.clear();
    }

    @NotNull
    @Override
    public Set<K> keySet() {
        return raw.keySet();
    }

    @NotNull
    @Override
    public Collection<V> values() {
        return raw.values();
    }

    @NotNull
    @Override
    public Set<Entry<K, V>> entrySet() {
        return raw.entrySet();
    }

    @Override
    public String toString() {
        String mapName = this.getClass().getSimpleName();
        List<String> entrys = new ArrayList<>();
        for (K key : this.keySet()) {
            entrys.add(key + "=" + get(key));
        }
        return mapName + "{" +
                Joiner.on(",").skipNulls().join(entrys.toArray()) +
                "}";
    }
}
